Skip to content

feat: Add CameraGestureObserver - reliable alternative to onMapIdle - #4074

Merged
mfazekas merged 9 commits into
mainfrom
onmapsteady
Nov 30, 2025
Merged

mfazekas merged 9 commits into
mainfrom
onmapsteady

Conversation

@mfazekas

@mfazekas mfazekas commented Nov 8, 2025 •

Copy link
Copy Markdown
Contributor

Problem

onMapIdle has fundamental issues as an upstream Mapbox SDK concept:

  1. Never called when LocationPuck pulse animation is running ([Bug]: Adding LocationPuck causes onMapIdle to be called infinitely #3801, [Bug]: (Android only) onMapIdle (etc) does not trigger with a native UserLocation #2902)
  2. Called excessively in other scenarios, causing performance issues ([Bug]: Adding LocationPuck causes onMapIdle to be called infinitely #3801)
  3. Unreliable for detecting when the map is truly ready/settled ([Bug]: onMapIdle never called #3701)

Since onMapIdle is an upstream concept, we cannot fix these issues directly.

Solution

Introduces CameraGestureObserver - a native component that reliably detects when the map reaches a steady state by tracking both camera gestures AND animations.

How it works

  • Monitors camera gesture lifecycle (pan, pinch, rotate, pitch)
  • Monitors camera animation lifecycle (including programmatic camera changes)
  • Emits onMapSteady event after a configurable quiet period (default 200ms)
  • Includes timeout mechanism for long-running activities

Usage

<MapView>
  <CameraGestureObserver
    quietPeriodMs={200}
    maxIntervalMs={5000}
    onMapSteady={({ nativeEvent }) => {
      console.log('Map is ready!', nativeEvent);
      // nativeEvent: { reason, idleDurationMs?, lastGestureType?, timestamp }
    }}
  />
</MapView>

Implementation

  • iOS: Uses GestureManagerDelegate + camera animator lifecycle observers
  • Android: Uses gesture listeners + CameraAnimationsLifecycleListener
  • Both platforms: Sophisticated state machine with quiet period and timeout logic

Related Issues

Fixes #3801
Fixes #2902
Fixes #3701

🤖 Generated with Claude Code

image
import React, { useCallback, useState } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { MapView, Camera, CameraGestureObserver } from '@rnmapbox/maps';

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  map: {
    flex: 1,
  },
  statusBar: {
    position: 'absolute',
    top: 50,
    left: 20,
    right: 20,
    backgroundColor: 'rgba(255, 255, 255, 0.95)',
    padding: 16,
    borderRadius: 8,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 2 },
    shadowOpacity: 0.25,
    shadowRadius: 3.84,
    elevation: 5,
  },
  title: {
    fontSize: 16,
    fontWeight: 'bold',
    marginBottom: 8,
  },
  statusText: {
    fontSize: 14,
    lineHeight: 20,
  },
  hint: {
    fontSize: 12,
    color: '#666',
    marginTop: 8,
    fontStyle: 'italic',
  },
});

const CameraGestureObserverExample = () => {
  const [status, setStatus] = useState('Waiting for interaction...');

  const onMapSteady = useCallback(({ nativeEvent }) => {
    const { reason, idleDurationMs, lastGestureType, timestamp } = nativeEvent;

    let message = `✓ Map is steady!\n\nReason: ${reason}`;

    if (reason === 'steady' && idleDurationMs !== undefined) {
      message += `\nIdle duration: ${Math.round(idleDurationMs)}ms`;
    }

    if (lastGestureType) {
      message += `\nLast gesture: ${lastGestureType}`;
    }

    message += `\nTime: ${new Date(timestamp).toLocaleTimeString()}`;

    console.log('[CameraGestureObserver]', nativeEvent);
    setStatus(message);
  }, []);

  return (
    <View style={styles.container}>
      <MapView style={styles.map}>
        <Camera
          defaultSettings={{
            centerCoordinate: [-74.006, 40.7128],
            zoomLevel: 12,
          }}
        />
        <CameraGestureObserver
          quietPeriodMs={200}
          maxIntervalMs={5000}
          onMapSteady={onMapSteady}
        />
      </MapView>
      <View style={styles.statusBar}>
        <Text style={styles.title}>Map Steady State</Text>
        <Text style={styles.statusText}>{status}</Text>
        <Text style={styles.hint}>
          Pan, zoom, or rotate the map to see the steady state detection
        </Text>
      </View>
    </View>
  );
};

export default CameraGestureObserverExample;

mfazekas and others added 7 commits November 8, 2025 07:17
Add native CameraGestureObserver component that detects when the map
has reached a steady state (no active gestures or animations).

Features:
- Emits onMapSteady event with reasons: 'steady' or 'timeout'
- Configurable quiet period (default 200ms) and max interval
- Returns last gesture type and idle duration
- Production-ready iOS and Android implementations

Example usage:
<CameraGestureObserver
  quietPeriodMs={200}
  maxIntervalMs={5000}
  onMapSteady={({ nativeEvent }) => {
    console.log('Map is steady:', nativeEvent.reason);
  }}
/>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add JSDoc comments to CameraGestureObserver props
- Run yarn generate to update docs
- Export OnMapSteadyEvent type for better TypeScript support

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add addGestureDelegate/removeGestureDelegate methods
- Support multiple gesture observers via NSHashTable
- Forward gesture events to all registered delegates
- Required for CameraGestureObserver to receive gesture events

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add CameraGestureObserver.tsx example demonstrating map steady state detection
- Register example in Map/index.js for navigation
- Restore BugReportExample.js to original state

Example features:
- Visual status overlay showing event data
- Display reason, idle duration, and last gesture type
- Console logging for debugging
- User hint for interaction

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@mfazekas
mfazekas merged commit cedd998 into main Nov 30, 2025
@mfazekas
mfazekas deleted the onmapsteady branch November 30, 2025 15:58
@Alvi24

Alvi24 commented Dec 4, 2025

Copy link
Copy Markdown

is there a way to get MapState onMapSteady?

@Alvi24

Alvi24 commented Feb 8, 2026

Copy link
Copy Markdown

having an issue with this component on Android:
Unsupported top level event type "topOnMapSteady" dispatched
See #4105

This branch was previously deployed

1 inactive deployment
default — d5b27423 Deployed Nov 30, 2025 by mfazekas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants